import dayjs from 'dayjs'

let instance
export default {
  type: 'crud',
  autoRequest: false,
  hasPaper: false,
  border: true,
  api: {
    url: `/payBill/info/findReconFile`,
    method: 'POST',
    data: {
      status: '0',
      billType: '01',
      bussType: 'API_COST'
    },
    responseAdaptor: (response) => {
      const dataSource = []
      response.data.forEach((item) => {
        const stmAgentDetailResps = []
        if (item.stmAgentDetailResps.length) {
          item.stmAgentDetailResps.forEach((stm, stmIndex) => {
            const billFileResp = stm.billFileResp || []
            if (billFileResp.length) {
              stmAgentDetailResps.push(
                ...billFileResp.map((billFile, index) => {
                  return {
                    ...item,
                    ...stm,
                    ...billFile,
                    rows: billFileResp,
                    firstRowspan: index === 0 ? item.stmAgentDetailResps * billFileResp.length : 0,
                    secondRowspan: index === 0 ? billFileResp.length : 1,
                  }
                })
              )
            } else {
              stmAgentDetailResps.push({
                ...item,
                ...stm,
                firstRowspan: stmIndex === 0 ? item.stmAgentDetailResps.length : 0,
                secondRowspan: 1,
              })
            }
          })
        } else {
          stmAgentDetailResps.push({
            ...item,
            firstRowspan: 1,
            secondRowspan: 1,
          })
        }
        dataSource.push(...stmAgentDetailResps)
      })
      response.data = dataSource
      return response
    }
  },
  spanMethod({ row, columnIndex }) {
    if (columnIndex === 0) {
      return {
        rowspan: row.firstRowspan,
        colspan: 1
      }
    } else if (columnIndex === 1 || columnIndex === 2 || columnIndex === 4 || columnIndex === 5) {
      return {
        rowspan: row.secondRowspan,
        colspan: 1
      }
    }
    return {
      rowspan: 1,
      colspan: 1
    }
  },
  onMounted(e) {
    instance = e.instance
  },
  querys: [
    {
      type: 'select',
      prop: 'suppliers',
      multiple: true,
      collapseTags: true,
      checkAll: true,
      label: '合作方',
      placeholder: '请选择合作方',
      api: {
        url: '/supplier/info/selectDifSupplierInfo?status=0&bussType=API_COST',
        responseAdaptor: (response) => {
          response.data = response.data.map((item) => ({
            label: item,
            value: item
          }))
          return response
        }
      },
      event: {
        loaded: (data) => {
          if (data && data.length) {
            instance.setupState.excuteQueryFn('updateDefaultValue', {
              suppliers: [data[0].value]
            })
            instance.setupState.queryEvent()
          }
        }
      }
    },
    {
      type: 'datePicker',
      prop: 'attribMonth',
      label: '归属月',
      placeholder: '请选择归属月',
      defaultValue: dayjs().format('YYYYMM'),
      'value-format': 'YYYYMM',
      reflectAttr: {
        type: 'month'
      }
    }
  ],
  columns: [
    {
      title: '合作方',
      field: 'supplier'
    },
    {
      title: '主体',
      field: 'stmAgent'
    },
    {
      title: '归属月',
      field: 'attribMonth'
    },
    {
      title: '文件名称',
      field: 'fileName'
    },
    {
      title: '制单人',
      field: 'userName'
    },
    {
      title: '操作',
      field: 'operation',
      width: '320px'
    }
  ]
}
